#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import time
import struct
import Packet
from operator import xor

VERSION = 4

CS_REQ_SERVER_ADDR    = 0x0206
SC_REQ_SERVER_ADDR    = 0x0207

CS_REQ_LOGIN = 0x090B
SC_REQ_LOGIN = 0x090CCS_REQ_QUEST_DATA    0x2000            /// 퀘스트 데이터 받음
{

}

CS_REQ_SLOT_LIST = 0x915
SC_REQ_SLOT_LIST = 0x915

CS_REQ_RANDOM_STAT = 0x938
SC_REQ_RANDOM_STAT = 0x939

CS_SLOT_PLAYER_CREATE = 0x0917
SC_SLOT_PLAYER_CREATE = 0x0918

CS_INPUTINFO = 0x0301
SC_INPUTINFO = 0x0302

CS_REQ_USER_INFO_INVEN = 0x0907
SC_REQ_USER_INFO_INVEN = 0x0908

CS_REQ_USER_INFO_DEFAULT = 0x0901
SC_REQ_USER_INFO_DEFAULT = 0x0902

SC_REQ_OPP_USER_INFO_DEFAULT_VILLAGE = 0x092F


CS_REQ_USER_INFO_QUICKSLOT =             0x0909
SC_REQ_USER_INFO_QUICKSLOT =             0x090A

CS_REQ_USER_INFO_INVEN_BAG =             0x0929
SC_REQ_USER_INFO_INVEN_BAG =             0x092A

CS_REQ_MY_LEVEL_EXP =         0x030A
SC_REQ_MY_LEVEL_EXP =         0x030B

CS_REQ_FAIRY_INFO =                     0x1500
SC_REQ_FAIRY_INFO =                     0x1501

CS_EVOLUTION_POINT_INFO =                  0x1800
SC_EVOLUTION_POINT_INFO =                  0x1801

CS_REQ_TITLE_DATA =                         0x1900
SC_REQ_TITLE_DATA =                         0x1901

CS_REQ_QUEST_DATA =                         0x2000
SC_REQ_QUEST_DATA =                         0x2001

CS_LOCATION_MODIFY =          0x0408
SN_LOCATION_MODIFY =          0x0409

class StressTest(Packet.Packet) :
    def __init__(self, host, port, socialid, logfile) :
        Packet.Packet.__init__(self, host, port, logfile)

        self.socialid = socialid
        self.encryptKey = socialid

        self.add_command(SC_REQ_SERVER_ADDR, self.SC_REQ_SERVER_ADDR)
        self.add_command(SC_REQ_LOGIN, self.SC_REQ_LOGIN)
        #self.add_command(SC_INPUTINFO, self.SC_INPUTINFO)

    def SC_REQ_SERVER_ADDR(self, packet) :
        packet = self.xorEncode(packet, self.socialid)
        self.encryptKey = packet[-4:]
        packet = Packet.parse_msg("%4d%2d%c%2d%4d", packet)
        print "SC_REQ_SERVER_ADDR u32ip : " + str(packet[0])
        print "SC_REQ_SERVER_ADDR u16port : " + str(packet[1])
        print "SC_REQ_SERVER_ADDR server_id : " + str(packet[2])
        print "SC_REQ_SERVER_ADDR u16channel_id : " + str(packet[3])
        print "SC_REQ_SERVER_ADDR encrypt_key : " + str(packet[4])       

    def SC_REQ_LOGIN(self, packet) :
        packet = self.xorEncode(packet, "ZenoniaOnline")
        packet = Packet.parse_msg("%2d", packet)
        print 'SC_REQ_LOGIN RESULT : ' + str(packet[0])
        
    #def SC_INPUTINFO(self, packet) :
        #print 'SC_INPUTINFO"
        #self.read_msg()

    def SC_ALL(self, packet) :
        print len(packet)



    def CS_REQ_SERVER_ADDR(self) :
        print 'CS_REQ_SERVER_ADDR'
        self.send_msg(CS_REQ_SERVER_ADDR, "", "%S", self.socialid)
        self.read_msg()

    def CS_REQ_LOGIN(self) :
        print 'CS_REQ_LOGIN'
        self.send_msg(CS_REQ_LOGIN, "ZenoniaOnline", "%2d%S%S%S%c", VERSION, self.socialid, "juno", "http://aaaa.jpg", 0)
        self.read_msg()
        
    def CS_REQ_SLOT_LIST(self):
        print 'CS_REQ_SLOT_LIST'
        self.send_msg(CS_REQ_SLOT_LIST, self.encryptKey, "")
        self.read_msg()
    
    def CS_REQ_RANDOM_STAT(self):
        print 'CS_REQ_RANDOM_STAT'
        self.send_msg(CS_REQ_RANDOM_STAT, self.encryptKey ,"%c%c", 1,1)
        self.read_msg()
        
    def CS_SLOT_PLAYER_CREATE(self):
        print 'CS_SLOT_PLAYER_CREATE'
        self.send_msg(CS_SLOT_PLAYER_CREATE, self.encryptKey ,"%c%c%c", 1,1,1)
        self.read_msg()
        
    def CS_INPUTINFO(self) :
        print 'CS_INPUTINFO'
        self.send_msg(CS_INPUTINFO, self.encryptKey ,"%c", 1)
        self.read_msg()
        
    def SC_REQ_OPP_USER_INFO_DEFAULT_VILLAGE(self) :
        print 'SC_REQ_OPP_USER_INFO_DEFAULT_VILLAGE'
        self.send_msg(SC_REQ_OPP_USER_INFO_DEFAULT_VILLAGE, self.encryptKey ,"")
        self.read_msg()
    
    def CS_REQ_USER_INFO_INVEN(self) :
        print 'CS_REQ_USER_INFO_INVEN'
        self.send_msg(CS_REQ_USER_INFO_INVEN, self.encryptKey ,"")
        self.read_msg()
        
    def CS_REQ_USER_INFO_DEFAULT(self) :
        print 'CS_REQ_USER_INFO_DEFAULT'
        self.send_msg(CS_REQ_USER_INFO_DEFAULT, self.encryptKey ,"%c", 1)
        #self.read_msg()
        
    def CS_REQ_USER_INFO_QUICKSLOT(self) :
        print 'CS_REQ_USER_INFO_QUICKSLOT'
        self.send_msg(CS_REQ_USER_INFO_QUICKSLOT, self.encryptKey ,"")
        self.read_msg()
        
    def CS_REQ_USER_INFO_INVEN_BAG(self) :
        print 'CS_REQ_USER_INFO_INVEN_BAG'
        self.send_msg(CS_REQ_USER_INFO_INVEN_BAG, self.encryptKey ,"%c%c", 1,1)
        self.read_msg()
        
    def CS_REQ_MY_LEVEL_EXP(self) :
        print 'CS_REQ_MY_LEVEL_EXP'
        self.send_msg(CS_REQ_MY_LEVEL_EXP, self.encryptKey ,"")
        self.read_msg()
        
    def CS_REQ_FAIRY_INFO(self) :
        print 'CS_REQ_FAIRY_INFO'
        self.send_msg(CS_REQ_FAIRY_INFO, self.encryptKey ,"%d", 100)
        #self.read_msg()
        
    def CS_EVOLUTION_POINT_INFO(self) :
        print 'CS_EVOLUTION_POINT_INFO'
        self.send_msg(CS_EVOLUTION_POINT_INFO, self.encryptKey ,"%d", 100)
        self.read_msg()
        
    def CS_REQ_TITLE_DATA(self) :
        print 'CS_REQ_TITLE_DATA'
        self.send_msg(CS_REQ_TITLE_DATA, self.encryptKey ,"")
        self.read_msg()
        
    def CS_REQ_QUEST_DATA(self) :
        print 'CS_REQ_QUEST_DATA'
        self.send_msg(CS_REQ_QUEST_DATA, self.encryptKey ,"")
        self.read_msg()
        
    def CS_LOCATION_MODIFY(self) :
        print 'CS_LOCATION_MODIFY'
        self.send_msg(CS_LOCATION_MODIFY, self.encryptKey ,"%d%c%2d%2d%2d%c", 100,1,1,12,1,1)
        #self.read_msg()

def usage() :
    print "usage : %s ip port socialid" % sys.argv[0]
    print "ex    : %s 0.0.0.0 40003 123412341234" % sys.argv[0]
    sys.exit()

if __name__ == "__main__" :
    if (len(sys.argv) < 4) : usage()

    #logpath = os.path.join("/home/jjaehuny/work/tmp", "projectz")
    #logpath = "/home/jjaehuny"
    logpath = "/home/mega"
    if (not os.path.exists(logpath)) :
        os.mkdir(logpath)

    logfile = os.path.join(logpath, sys.argv[3] + ".log")

    client = StressTest(sys.argv[1], int(sys.argv[2]), sys.argv[3], logfile)
    
    while (True) :
        client.CS_REQ_SERVER_ADDR()
        client.CS_REQ_LOGIN()
        client.CS_REQ_SLOT_LIST()
        client.CS_REQ_RANDOM_STAT()
        client.CS_SLOT_PLAYER_CREATE()
        client.CS_INPUTINFO()
        #client.SC_REQ_OPP_USER_INFO_DEFAULT_VILLAGE()
        client.CS_REQ_USER_INFO_INVEN()
        client.CS_REQ_USER_INFO_DEFAULT()
        client.CS_REQ_USER_INFO_QUICKSLOT()
        client.CS_REQ_USER_INFO_INVEN_BAG()
        client.CS_REQ_MY_LEVEL_EXP()
        client.CS_REQ_FAIRY_INFO()
        client.CS_EVOLUTION_POINT_INFO()
        client.CS_REQ_TITLE_DATA()
        client.CS_REQ_QUEST_DATA()
        client.CS_LOCATION_MODIFY()
        time.sleep(1)
    
    
